//
//  CartoonMainViewController.m
//  Double
//
//  Created by 袁庆秋 on 15/5/22.
//  Copyright (c) 2015年 袁庆秋. All rights reserved.
//

#import "CartoonMainViewController.h"
#import "SubjectViewController.h"
#import "RecommendViewController.h"
#import "CartoonClassifyViewController.h"
#import "SearchViewController.h"
@interface CartoonMainViewController ()<UIScrollViewDelegate>

@property (nonatomic, retain) UISegmentedControl *segment;
@property (nonatomic, retain) UIScrollView *scroll;


@end

@implementation CartoonMainViewController

- (void)dealloc
{
    [_segment release];
    [_scroll release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

    }
    return self;
}

- (void)loadView
{
    [super loadView];
    [self createScrollView];
    [self createSegment];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.titleView = _segment;
    

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search.png"] style:UIBarButtonItemStyleDone target:self action:@selector(searchAction:)];
    
}

- (void)searchAction:(id)sender
{
    SearchViewController *search = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:search animated:YES];
}

#pragma mark -
#pragma mark 创建scrollView
- (void)createScrollView
{
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.scroll.contentSize = CGSizeMake(self.view.frame.size.width * 3, 0);
    self.scroll.pagingEnabled = YES;
    self.scroll.delegate = self;
//片单页面偏移量
    SubjectViewController *subject = [[SubjectViewController alloc] init];
    subject.view.frame = CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self addChildViewController:subject];
//推荐页面偏移量
    RecommendViewController *recommend = [[RecommendViewController alloc] init];
    recommend.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self addChildViewController:recommend];
//分类页面偏移量
    CartoonClassifyViewController *cartoon = [[CartoonClassifyViewController alloc] init];
    cartoon.view.frame = CGRectMake(self.view.frame.size.width * 2, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self addChildViewController:cartoon];
    
    [self.scroll addSubview:subject.view];
    [self.scroll addSubview:recommend.view];
    [self.scroll addSubview:cartoon.view];
    
    
    [self.view addSubview:self.scroll];
    
    [self.scroll release];
}

#pragma mark -
#pragma mark 创建segmentControl
- (void)createSegment
{
    NSArray *array = [NSArray arrayWithObjects:@"推荐",@"片单",@"分类", nil];
    self.segment = [[UISegmentedControl alloc] initWithItems:array];
    self.segment.frame = CGRectMake(40, 0, self.view.frame.size.width / 2, 38);
    self.segment.selectedSegmentIndex = 0;
    self.segment.layer.cornerRadius = 20;
    
    [self.segment addTarget:self action:@selector(segmentChanged:) forControlEvents:UIControlEventValueChanged];
    

}

#pragma mark -
#pragma mark scrollView协议
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.x == 0) {
        self.segment.selectedSegmentIndex = 0;
    } else if (scrollView.contentOffset.x == self.scroll.frame.size.width){
        self.segment.selectedSegmentIndex = 1;
    } else if (scrollView.contentOffset.x == self.scroll.frame.size.width * 2){
        self.segment.selectedSegmentIndex = 2;
    }
}



#pragma mark -
#pragma mark segmentControl 点击事件
- (void)segmentChanged:(UISegmentedControl *)seg
{
    switch (seg.selectedSegmentIndex) {
        case 0:
            self.scroll.contentOffset = CGPointMake(0, 0);
            break;
        case 1:
            self.scroll.contentOffset = CGPointMake(self.scroll.frame.size.width, 0);
            break;
        case 2:
            self.scroll.contentOffset = CGPointMake(self.scroll.frame.size.width * 2, 0);
            break;
        default:
            break;
    }
}

@end
